package beer.xz.beer.device.ui.test

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@SuppressLint("UnrememberedMutableState")
@OptIn(ExperimentalMaterial3Api::class, ExperimentalStdlibApi::class)
@Composable
fun TestScreen() {
    Box(modifier = Modifier.fillMaxSize()) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 20.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            val commands =
                listOf(Pair("命令1", "0x10"), Pair("命令2", "0x11"), Pair("命令3", "0x12"))
            Text("串口测试", fontSize = 32.sp)


            Column(
                modifier = Modifier.fillMaxWidth(),
                verticalArrangement = Arrangement.SpaceEvenly // 均分垂直空间
            ) {
                val volume = remember { mutableIntStateOf(1) }

                val data = remember { mutableStateOf("") }
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
//                        .weight(1f) // 分配权重
                        .padding(10.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(10.dp)
                ) {
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally,
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Text("毫升数：", fontSize = 20.sp)
                            TextField(
                                value = volume.intValue.toString(),
                                onValueChange = {
                                    if (it.isEmpty() || it.all { char -> char.isDigit() }) {
                                        volume.intValue = it.toIntOrNull() ?: 0
                                    }
                                },
                                keyboardOptions = KeyboardOptions(
                                    keyboardType = KeyboardType.Number, imeAction = ImeAction.Done
                                ),
                                visualTransformation = VisualTransformation.None,
                            )
                        }

//                            ExposedDropdownMenuBox(
//                                expanded = expanded.value,
//                                onExpandedChange = { expanded.value = it }
//                            ) {
//
//
//
//                                TextField(
////                                label = { Text("Select Command") },
//
//                                    value = text.value,
//                                    onValueChange = { text.value = it },
//                                    textStyle = LocalTextStyle.current.copy(fontSize = 20.sp), // 设置字体大小
//                                    readOnly = true,
//                                    modifier = Modifier
//                                        .menuAnchor(MenuAnchorType.PrimaryNotEditable)
////                            .fillMaxWidth()
//                                        .padding(8.dp)
//                                )
//
//                                ExposedDropdownMenu(
//                                    expanded = expanded.value,
//                                    onDismissRequest = { expanded.value = false }
//                                ) {
//                                    commands.forEach { command ->
//                                        DropdownMenuItem(
//                                            text = { Text(command.first) },
//                                            onClick = {
//                                                text.value = command.first
//                                                expanded.value = false
//                                                cmdSelected.value=command.second
//                                            }
//                                        )
//                                    }
//                                }
//                            }
//                    }
                        TextField(
                            value = data.value,
                            label = { Text("待发送的命令") },
                            readOnly = true,
                            minLines = 2,
                            onValueChange = {},
                            modifier = Modifier
                                .padding(top = 10.dp)
                                .height(60.dp)
                        )
                        Button(onClick = { }, modifier = Modifier.padding(top = 10.dp)) {
                            Text(
                                "Send", fontSize = 20.sp
                            )
                        }
                    }


                }
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(10.dp)
                ) {
                    Column(modifier = Modifier.fillMaxWidth()) {
                        TextField(
                            label = { Text("串口收到的数据") },
                            readOnly = true,
                            value = "",
                            onValueChange = {})
                    }
                }

                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(10.dp)
                ) {
                    Column(modifier = Modifier.fillMaxWidth()) {

                        TextField(value = "", onValueChange = {}, label = { Text("任意数据测试") })
                    }

                }
            }
        }
    }
}


@Preview
@Composable
fun TestPreview() {
    TestScreen()
}